//
//  XCLeftSortsViewController.m
//  SoundOfNature
//
//  Created by dllo on 16/3/10.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCLeftSortsViewController.h"
#import "XCNightTableViewCell.h"
#import "CleanCaches.h"
@interface XCLeftSortsViewController () <UITableViewDataSource, UITableViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIImageView *myHeadImageView;
@property (nonatomic, retain) UIImagePickerController *imagePicker;
@property (nonatomic, retain) NSString *mb;


@end

@implementation XCLeftSortsViewController
- (void)dealloc {
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    _imagePicker.delegate = nil;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [_tableView release];
    [_myHeadImageView release];
    [_imagePicker release];
    [_mb release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.169 green:0.950 blue:1.000 alpha:1.000];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 3, 0, wide / 2, self.view.bounds.size.height / 4) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"q"];
    [self.tableView registerClass:[XCNightTableViewCell class] forCellReuseIdentifier:@"Identifier"];
    [self.view addSubview:self.tableView];
    
    self.mb = [CleanCaches folderSizeAtPath];
    
    [self.tableView reloadData];
//    获取消息中心
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//    添加观察者
    [center addObserver:self selector:@selector(receiveNotification:) name:@"stringFloderSize" object:nil];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return wide / 2;
    }
    return wide / 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"q"];
        cell.backgroundColor = [UIColor colorWithRed:0.472 green:0.677 blue:1.000 alpha:1.000];
        return cell;
    }
    if (indexPath.row == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"q"];
        
        cell.imageView.image = [[UIImage imageNamed:@"Image-landing"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        cell.textLabel.text = @"登陆/注册";
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        cell.backgroundColor = [UIColor colorWithRed:0.472 green:0.677 blue:1.000 alpha:1.000];
        return cell;
    } else if (indexPath.row == 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"q"];
        
        cell.imageView.image = [[UIImage imageNamed:@"Image-backImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        cell.textLabel.text = @"更换壁纸";
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        cell.backgroundColor = [UIColor colorWithRed:0.472 green:0.677 blue:1.000 alpha:1.000];
        return cell;
    } else if (indexPath.row == 3) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"q"];
        
        cell.imageView.image = [[UIImage imageNamed:@"Image-clear"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
       
//      获取完整路径
//        NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
//        NSString *cachePath = [path stringByAppendingPathComponent:@"Caches"];
        
        //及时更新label上文件的大小
//        self.mb = [NSString stringWithFormat:@"%.2fM", [self folderSizeAtPath:cachePath]];
//        _mb = [CleanCaches folderSizeAtPath];
        cell.textLabel.text = [NSString stringWithFormat:@"清除缓存            %@",self.mb];
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        cell.backgroundColor = [UIColor colorWithRed:0.472 green:0.677 blue:1.000 alpha:1.000];
        
        return cell;
    } else {
        XCNightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];
        [cell.switchBar addTarget:self action:@selector(switchBarAction:) forControlEvents:UIControlEventTouchUpInside];
       cell.backgroundColor = [UIColor colorWithRed:0.472 green:0.677 blue:1.000 alpha:1.000];
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"nightPath"] isEqualToString:@"0"]) {
            cell.switchBar.on = NO;
        } else {
            cell.switchBar.on = YES;
        }
        return cell;
    }

    

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登陆注册" message:@"本版本不支持登陆注册将在下个版本发布" preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];

        [alert addAction:sure];
        [self presentViewController:alert animated:YES completion:^{
            
        }];

    } else if (indexPath.row == 2) {

            self.imagePicker = [[UIImagePickerController alloc] init];
            //    设置图片来源
            self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            self.imagePicker.delegate = self;
            self.imagePicker.allowsEditing = YES;
            [self presentViewController:self.imagePicker animated:YES completion:^{
                
            }];
        [_imagePicker release];
        
    } else if (indexPath.row == 3) {

        self.mb = [CleanCaches cleanCachesAtPath];
        [self.tableView reloadData];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}
// 从相册取照片的协议方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    UIImage *image = info[@"UIImagePickerControllerEditedImage"];
    self.myHeadImageView.image = image;
//    将对象存入本地
    NSArray *liabraryPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    
    NSString *liabraryPath = [liabraryPaths lastObject];
    liabraryPath = [liabraryPath stringByAppendingString:@"/lxc.plist"];
    [NSKeyedArchiver archiveRootObject:image toFile:liabraryPath];

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"image" object:liabraryPath userInfo:nil];
    
    
}
// 夜间模式
- (void)switchBarAction:(UISwitch *)sender {
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    if (sender.on == YES) {
       [center postNotificationName:@"day" object:[NSString stringWithFormat:@"%d", sender.on] userInfo:nil];
    } else if (sender.on == NO) {
       [center postNotificationName:@"day" object:[NSString stringWithFormat:@"%d", sender.on] userInfo:nil];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%d", sender.on] forKey:@"nightPath"];

}

- (void)receiveNotification:(NSNotification *)message {
    self.mb = message.object;
    [self.tableView reloadData];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
